package com.mo.xi.ge.info.help

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Build
import android.provider.Settings
import android.telephony.CellInfoCdma
import android.telephony.CellInfoGsm
import android.telephony.CellInfoLte
import android.telephony.CellInfoNr
import android.telephony.CellInfoWcdma
import android.telephony.TelephonyManager
import androidx.core.app.ActivityCompat


/**
 * date:2024/1/25
 **/
internal object DeviceInfoUtil {

    /** 获取android id */
    @JvmStatic
    @SuppressLint("HardwareIds")
    fun getAndroidId(context: Context): String {
        return infoTryCatch("") {
            Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
        }
    }

    /** imei */
    @SuppressLint("MissingPermission")
    @JvmStatic
    fun getSysImei(context: Context): String {
        var reult: String? = ""
        if (Build.VERSION.SDK_INT >= 29) { // 无论是否有权限，都会报SecurityException
            return reult.orEmpty()
        }
        if (!context.checkAppPermission(Manifest.permission.READ_PHONE_STATE)) {
            return reult.orEmpty()
        }
        try {
            reult = getAllImei(context).firstOrNull()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return reult.orEmpty()
    }

    private fun getAllImei(context: Context): ArrayList<String> {
        val imeiList = arrayListOf<String>()
        if (Build.VERSION.SDK_INT >= 29) { // 无论是否有权限，都会报SecurityException
            return imeiList
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (context.checkAppPermission(Manifest.permission.READ_PHONE_STATE)) {
                try {
                    val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                    val phoneCount = tm.phoneCount
                    for (index in 0 until phoneCount) {
                        val imei = tm.getImei(index)
                        if (imei.isNullOrEmpty()) {
                            continue
                        }
                        imeiList.add(imei)
                    }
                } catch (e: Exception) {

                }
            }
        }
        return imeiList
    }

    // 是否usbdebug模式
    fun isUsbDebug(context: Context): Boolean = try {
        Settings.Secure.getInt(context.contentResolver, Settings.Global.ADB_ENABLED) > 0
    } catch (e: Exception) {
        false
    }

    // 是否飞行模式
    fun isAirModeOn(context: Context): Boolean = try {
        Settings.Secure.getInt(
            context.contentResolver,
            Settings.Global.AIRPLANE_MODE_ON,
            0
        ) == 1
    } catch (e: Exception) {
        false
    }

    private fun hasSimCard(context: Context): Boolean {
        val telMgr = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val simState = telMgr.simState
        var result = true
        when (simState) {
            TelephonyManager.SIM_STATE_ABSENT, TelephonyManager.SIM_STATE_UNKNOWN -> result =
                false // 没有SIM卡
        }
        return result
    }

    fun getBluetoothName(context: Context): String? {
        if (ActivityCompat.checkSelfPermission(
                context, Manifest.permission.BLUETOOTH_CONNECT
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            try {
                return BluetoothAdapter.getDefaultAdapter()?.name
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return ""
    }

    // 模拟定位
    @SuppressLint("ServiceCast")
    fun isMockLocaEnable(context: Context): Int {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            try {
                val locam = context.getSystemService(Context.LOCATION_SERVICE) as? LocationManager
                if (locam?.isLocationEnabled == true) 0 else 1
            } catch (e: Exception) {
                -1
            }
        } else {
            try {
                Settings.Secure.getInt(context.contentResolver, Settings.Secure.ALLOW_MOCK_LOCATION)
            } catch (e: Exception) {
                -1
            }
        }
    }


    //数据漫游
    fun isDataRoaming(context: Context): Boolean {
        return try {
            val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            tm.isNetworkRoaming
        } catch (e: Exception) {
            false
        }
    }

    // 默认输入法
    fun getDefInput(context: Context): String {
        return try {
            Settings.Secure.getString(context.contentResolver, Settings.Secure.DEFAULT_INPUT_METHOD)
        } catch (e: Exception) {
            ""
        }
    }

    // 触摸检测
    fun isTouchExplorEnable(context: Context): Boolean {
        return try {
            Settings.Secure.getInt(
                context.contentResolver,
                Settings.Secure.TOUCH_EXPLORATION_ENABLED
            ) == 1
        } catch (e: Exception) {
            false
        }
    }

    // 开发者模式
    fun isDevelop(context: Context): Boolean {
        return try {
            Settings.Secure.getInt(
                context.contentResolver,
                Settings.Global.DEVELOPMENT_SETTINGS_ENABLED
            ) == 1
        } catch (e: Exception) {
            false
        }
    }

    // 屏幕时间
    fun getScreenTime(context: Context): Long {
        return try {
            Settings.System.getLong(context.contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
        } catch (e: Exception) {
            -1
        }
    }


    @SuppressLint("MissingPermission")
    fun getMobileDbm(context: Context): Int {
        var dbm: Int? = null
        try {
            if (!hasSimCard(context)) {
                return -1
            }
            val telephonyManager =
                context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
                    ?: return -1
            val allCellInfo = telephonyManager.allCellInfo ?: return -1

            for (cellInfo in allCellInfo) {
                when (cellInfo) {
                    is CellInfoGsm -> {
                        dbm = cellInfo.cellSignalStrength.dbm
                    }

                    is CellInfoCdma -> {
                        dbm = cellInfo.cellSignalStrength.dbm;
                    }

                    is CellInfoWcdma -> {
                        dbm = cellInfo.cellSignalStrength.dbm
                    }

                    is CellInfoLte -> {
                        dbm = cellInfo.cellSignalStrength.dbm
                    }

                    else -> {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && cellInfo is CellInfoNr) {
                            dbm = cellInfo.cellSignalStrength.dbm
                        }
                    }


                }

            }
        } catch (e: Throwable) {

        }
        return dbm ?: -1
    }
}
